/**
 * Copyright:Copyright (C) 2014-2017
 * Cpmpany:安徽东位信息科技有限责任公司
 * All Rights Reserved
 * 
 * 名称：DataLinkSubServer Demo
 * 版本：V0.0.1
 * 
 * Author:Riven
 * QQ:508620006
 * 
 * Describe:
 * 对订阅到的数据经行处理，这里数据包含 经纬度数据和设备状态数据，可根据自己的业务实际场景来开发
 * 通常在这里经行数据入库操作，设备在线 离线消息维护，推送等操作
 * 
 */
package com.lbssoft.gpsbd.datalink;

import java.io.IOException;
import java.text.SimpleDateFormat;

import com.alibaba.fastjson.JSON;
import com.lbssoft.datalink.model.*;
import com.lbssoft.datalink.util.Log;
import com.lbssoft.gpsbd.controller.WebSocket;

public class DataHandler {
	
	
	Position position;

	public void Handler(MsgType msgType, BaseMsg baseMsg) {

		switch (msgType) {
			case POSITION: {
					Position position = (Position) baseMsg;
					Log.info(String.format("时间:%s,设备号：%s,经度：%f,纬度:%f,海拔:%f,速度:%f,方向:%f,属性:%s,基站:%s%n",
							new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(position
									.getTime()), position.getUniqueId(), position
									.getLatitude(), position.getLongitude(), position
									.getAltitude(), position.getSpeed(), position
									.getCourse(), position.getAttributes(), position
									.getNetwork()));

					 Context.dataManager.updateLastPosition(position);
					
				}
				break;
			case EVENT: {
					Event event = (Event) baseMsg;
				Log.info(JSON.toJSONString(event));
				Log.info(String.format("时间:%s,设备号:%s,状态：%s%n", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(event.getTime()), event.getUniqueId(), event.getStatus()));
					//if(event.getStatus() == 1 || event.getStatus() == 0 || event.getStatus() == -1){
						//Context.dataManager.updateStatus(event.getStatus(), event.getDeviceUUID());
					//}
					Context.dataManager.addEvent(event);
				
					
	
				}
			
			break;
//			case ATTRIBUTES: {
//				Attribute attributes = (Attribute) baseMsg;
//				Log.info("处理属性："+JSON.toJSONString(attributes));
//				Context.dataManager.updateState(attributes.getUniqueId(),attributes.getAttributes());
//
//			}
//
//			break;
			case COMMAND: {
				Command command = (Command) baseMsg;
				Log.info("处理命令回复："+JSON.toJSONString(command));
				//Log.info("设备号："+command.getUniqueId()+" 命令响应内容"+command.getRes()+" 状态"+command.getResult());
				Context.dataManager.updateCommandRecord(command);
			}
			break;
			default:
	
				break;
		}

	}
	

	

}
